
//
//  LYTabBar.m
//  iphoneApp
//
//  Created by Liyu on 13-1-21.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import "LYTabBar.h"

@implementation LYTabBar


@synthesize tabBarItmesArray,backgroundImageView,buttons,delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor clearColor];
        
        self.backgroundImageView=[[[UIImageView alloc]  initWithImage:[self defautlBackGroundImageView]] autorelease];
//         self.backgroundImageView=[[[UIImageView alloc]  initWithImage:[UIImage imageNamed:@"bottombar.png"]] autorelease];
        self.backgroundImageView.contentMode = UIViewContentModeBottom;
        self.backgroundImageView.center = CGPointMake(self.center.x, self.frame.size.height/2);
        //self.backgroundImageView.backgroundColor=[UIColor clearColor];
        //autoresizingMask属性为确认视图大小随父级大小改变自适应的方式
        self.backgroundImageView.autoresizingMask=
        UIViewAutoresizingFlexibleWidth |
        UIViewAutoresizingFlexibleHeight |
        UIViewAutoresizingFlexibleLeftMargin | 
        UIViewAutoresizingFlexibleRightMargin;
        [self addSubview:self.backgroundImageView];
        
    }
    return self;
}

-(UIImage *) defautlBackGroundImageView
{
    
    CGFloat width=[[UIScreen mainScreen] bounds].size.width;
    UIImage * originalImg=[UIImage imageNamed:@"tabBarGradient.png"];
    
    //创建一个位图上下文，用于绘图
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, self.frame.size.height), NO, 0.0);  
    
    UIImage * stretchedImg=[originalImg stretchableImageWithLeftCapWidth:0 topCapHeight:0 ];
    [stretchedImg drawInRect:CGRectMake(0, 0, width, originalImg.size.height)];
    
    //在当前位图上下文的指定区域填充指定颜色
//    [[UIColor colorWithWhite:1.0 alpha:0.3] set];
//    CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, self.frame.size.height / 2, width, self.frame.size.height / 2));
    
    UIImage * resultImg=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return  resultImg;
}

-(void)setTabBarWithItems:(NSArray *) tabBarItems
{
    CGFloat offset=0.0f;
    CGSize buttonSize=CGSizeMake((self.frame.size.width-offset*2)/[tabBarItems count], 
                                 self.frame.size.height);
    NSMutableArray * buttonArr=[NSMutableArray array];
    
    
    
    for (int i=0; i<[tabBarItems count]; i++) {
        
        UITabBarItem * item=(UITabBarItem *)[tabBarItems objectAtIndex:i];
        UIButton * button=[[[UIButton alloc] initWithFrame:CGRectMake(i*buttonSize.width+offset, 0, buttonSize.width, buttonSize.height)] autorelease];
        button.tag=i;
        [button addTarget:self action:@selector(tabBarItemDidSelect:) forControlEvents:UIControlEventTouchUpInside];
        [button setImage:item.image forState:UIControlStateNormal];
        [self addSubview:button];
        [buttonArr addObject:button];
    }
    
    self.buttons=buttonArr;
    
}

-(void)tabBarItemDidSelect:(UIButton *) button
{
    if(self.delegate!=nil &&
       [self.delegate respondsToSelector:@selector(lyTabBar:didSelectItemIndex:)])
    {
        NSInteger index=button.tag;
        [self.delegate lyTabBar:self didSelectItemIndex:index];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
